//
//  HLTsearchlistViewController.m
//  huiliantong
//
//  Created by hlt on 15/10/13.
//  Copyright © 2015年 hlt. All rights reserved.
//

#import "HLTsearchlistViewController.h"
#import "HLTShopViewController.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "RecommendCell.h"
//#import "SearchViewController.h"
//#import "cloudsearchmapViewController.h"
//#import <BaiduMapAPI/BMapKit.h>
//#import <CoreLocation/CoreLocation.h>
@interface HLTsearchlistViewController ()
{
    NSMutableArray *_recommendArray;
    NSMutableArray *_MerchantArray;
    NSString *_locationInfoStr;
    NSInteger _KindID;//分类查询ID，默认-1
    NSInteger _offset;
    
    
    
}
@property (strong, nonatomic) NSString *lat;
@property (strong, nonatomic) NSString *log;

@end




@implementation HLTsearchlistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _MerchantArray = [[NSMutableArray alloc] init];
    _recommendArray = [[NSMutableArray alloc] init];   //商铺数据
//    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
        [self initViews];
    
    
    
    
    
    
   
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)initViews{
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self setUpTableView];
    //去掉多余分割线的方法
    UIView *view = [UIView new];
    
    view.backgroundColor = [UIColor clearColor];
    
    [self.tableView setTableFooterView:view];
}



-(void)setUpTableView{
    //添加下拉的动画图片
    //设置下拉刷新回调
    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
    
    //设置普通状态的动画图片
//    NSMutableArray *idleImages = [NSMutableArray array];
//    for (NSUInteger i = 1; i<=60; ++i) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_anim__000%zd",i]];
//        [idleImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:idleImages forState:MJRefreshHeaderStateIdle];
//    
//    //设置即将刷新状态的动画图片
//    NSMutableArray *refreshingImages = [NSMutableArray array];
//    for (NSInteger i = 1; i<=3; i++) {
//        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"dropdown_loading_0%zd",i]];
//        [refreshingImages addObject:image];
//    }
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStatePulling];
//    
//    //设置正在刷新是的动画图片
//    [self.tableView.gifHeader setImages:refreshingImages forState:MJRefreshHeaderStateRefreshing];
    
    //马上进入刷新状态
    [self.tableView.gifHeader beginRefreshing];
    
    
    //上拉刷新
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    //隐藏状态文字
    //    self.tableView.footer.stateHidden = YES;
    //设置正在刷新的动画
//    self.tableView.gifFooter.refreshingImages = refreshingImages;
    
    
    
    
}


-(void)getFirstPageData{
    _offset = 0;
    [self refreshData];
}

-(void)refreshData{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self getMerchantList];
    });
}

-(void)loadMoreData{
    NSLog(@"2222  offset:%zd",_offset);
    _offset = _offset + 10;
    NSLog(@"3333  offset:%zd",_offset);
    [self refreshData];
}


//获取商家列表
-(void)getMerchantList{
    //    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    NSInteger page1=_offset/10;
    //周边商铺列表(x=%@&y=%@&page=0)
    NSString *url;
    
    
        url=[searchshop stringByAppendingFormat:@"&keyword=%@&page=%ld",self.keyname,(long)page1];
    
      
        
        [HLTNetWorkingtool getyouhuilist:url success:^(NSMutableArray *ary) {
            if (_offset<10) {
                [_recommendArray removeAllObjects];
            }
            for (int i = 0; i < ary.count; i++) {
                
                RecommendModel *recommend = [RecommendModel objectWithKeyValues:ary[i]];
                [_recommendArray addObject:recommend];
                
            }
            [self.tableView reloadData];
            //刷新指定的section,显示推荐商铺的部分
            //        NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
            //        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        } faliure:^(NSString *error) {
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        }];
        
    
}





#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _recommendArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}




-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *cellIndentifier = @"recommendcell";
    RecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    
    if (cell == nil) {
        cell = [[RecommendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if(_recommendArray.count!=0){
        
        
            
            RecommendModel *recommend = _recommendArray[indexPath.row];
            [cell setRecommendData:recommend];
        
        
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
    
    
    
    
    
}



#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        RecommendModel *recommend = _recommendArray[indexPath.row];
        //                    [recommend objectArray2keyValuesArray]
        //                    NSString *shopId = recommend.Shop_id;
        //            NSLog(@"shop id:%@",shopId);
        
        HLTShopViewController *shopvc=[[HLTShopViewController alloc]init];
        shopvc.shopcontent=recommend;
        shopvc.biaozhi=YES;
        //隐藏tabbar
        shopvc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:shopvc animated:YES];
        //                    HLTShopcontentViewController *shopVC = [[HLTShopcontentViewController alloc] init];
        //                    shopVC.imagearray=imagearray;
        //                    shopVC.shopID = shopId;
        //                    shopVC.shopcontent=recommend;
        //                    [self.navigationController pushViewController:shopVC animated:YES];
        
    
    
    
    
    
    
    
    
    
}




//加载页面时执行的方法
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    
}






@end

